Skip to content

fix: compile and instrument TypeScript in use-babelrc unit tests - #1013

Merged
jennifer-shehane merged 2 commits into
masterfrom
claude/issue-435-triage-verify-r69b19
Nov 2, 2026
Merged

jennifer-shehane merged 2 commits into
masterfrom
claude/issue-435-triage-verify-r69b19

Conversation

@jennifer-shehane

@jennifer-shehane jennifer-shehane commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Additional details

@cypress/code-coverage/use-babelrc only ran Babel on .js/.jsx files, and webpack couldn't resolve .ts imports. So TypeScript unit-test specs failed to bundle (Module parse failed: Unexpected token), and .ts sources imported from JS specs couldn't be resolved, which meant no coverage for TS unit tests. The old examples/unit-tests-ts app the issue refers to no longer exists, and the README's test-apps/unit-tests-ts entry pointed at ts-example instead.

What changed:

  • lib/use-babelrc.ts: the Babel rule now also matches .ts/.tsx, so the project's .babelrc compiles them. .jsx, .ts and .tsx are added to resolve.extensions after webpack's defaults (.js, .json, .wasm), so existing JS imports resolve exactly as before. The two @ts-ignores are replaced with proper type narrowing.
  • New unit test: test/lib/use-babelrc.test.ts bundles a TS fixture through the real preprocessor and checks that the imported .ts file is instrumented. It fails without the fix.
  • New test app: test-apps/unit-tests-ts (the TS twin of unit-tests-js) is added to the verify-test-apps CI matrix. The README entry now links to it.
  • README: the "Instrument unit tests" section explains adding @babel/preset-typescript to .babelrc (plus a JSX preset for .tsx).
  • package.json: @babel/preset-typescript is a new root devDependency, since the test apps resolve packages from the repo root.

This isn't a breaking change. .ts files with type syntax didn't compile with use-babelrc before, and .ts files without type syntax still compile through Babel even without the TypeScript preset.

The underlying problem overlaps with #361, which also covers the browserify preprocessor and is left open.

Steps to test

  1. npm ci && npm run build && npm test. The new use-babelrc test passes.
  2. cd test-apps/unit-tests-ts && npm i && npm test && npm run coverage:verify && npm run coverage:check-files. You should see 100% line coverage, with only misc.ts and math.ts in the report.
  3. Optionally, repeat step 2 in test-apps/unit-tests-js to confirm JS unit tests are unaffected.

I couldn't run cypress run where I prepared this (the Cypress binary download was blocked), so the verify-test-apps (unit-tests-ts) CI job is the first real Cypress run of the new app. Both apps' specs were bundled and instrumented through the preprocessor directly.

How has the user experience changed?

Before: with on('file:preprocessor', require('@cypress/code-coverage/use-babelrc')), TypeScript specs failed to bundle and .ts source files got no coverage.

After: TypeScript specs and the .ts files they import are compiled with the project's .babelrc and show up in the coverage report, once @babel/preset-typescript is in .babelrc. No visual change.


Generated by Claude Code


Note

Low Risk
Focused preprocessor/webpack config change with regression tests; JavaScript unit-test behavior is intended to stay the same.

Overview
Fixes TypeScript unit tests that use @cypress/code-coverage/use-babelrc, where specs and imported .ts sources failed to bundle or appear in coverage because webpack only ran Babel on .js/.jsx and could not resolve .ts imports.

lib/use-babelrc.ts widens the webpack Babel rule to /\.[jt]sx?$/, appends .jsx, .ts, and .tsx to resolve.extensions (after webpack’s defaults), and replaces @ts-ignore with typed checks while still stripping built-in presets so the project .babelrc drives compilation.

Docs and examples: README’s “Instrument unit tests” section now documents @babel/preset-typescript in .babelrc and points to a dedicated test-apps/unit-tests-ts app (replacing the incorrect link to ts-example). CI verify-test-apps includes unit-tests-ts.

Tests: A Vitest case bundles a TypeScript fixture through the real preprocessor and asserts Istanbul instrumentation on imported .ts files; root @babel/preset-typescript supports fixtures and the new app.

Reviewed by Cursor Bugbot for commit cdd8906. Bugbot is set up for automated code reviews on this repo. Configure here.

`@cypress/code-coverage/use-babelrc` only ran Babel on `.js`/`.jsx` files
and webpack could not resolve `.ts` imports, so TypeScript unit-test specs
failed to bundle ("Module parse failed") and TypeScript sources imported
by specs were never instrumented.

- Run the `.babelrc`-driven babel-loader rule on `.ts`/`.tsx` as well
- Resolve `.jsx`, `.ts` and `.tsx` after webpack's default extensions
- Add test-apps/unit-tests-ts (and to the CI matrix), and point the
  README's "unit-tests-ts" entry at it instead of ts-example
- Document adding `@babel/preset-typescript` to `.babelrc`

Fixes #435

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WUv8r36Qgb8PyPw9VYGEbP
- Narrow the babel-loader rule instead of using @ts-ignore
- Remove the test's temp output folder after each run
- Note that .tsx files also need a JSX preset in .babelrc

Refs #435

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WUv8r36Qgb8PyPw9VYGEbP
@jennifer-shehane
jennifer-shehane merged commit a29d750 into master Nov 2, 2026
33 checks passed
@jennifer-shehane
jennifer-shehane deleted the claude/issue-435-triage-verify-r69b19 branch November 2, 2026 00:42
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 4.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

example/unit-tests-ts is not producing coverage for unit-test files

2 participants